package com.taplinker.client.crm.rpc;

import android.widget.Toast;

import com.google.protobuf.InvalidProtocolBufferException;
import com.taplinker.client.AppConfigContext;
import com.taplinker.client.crm.domain.CrmDataCenter;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
import com.taplinker.dto.web.FindMessageListRequest;
import com.taplinker.dto.web.FindMessageListRequest.FindMessageListReq.Builder;
import com.taplinker.dto.web.MessageListResponse;
/**
 * 获取聊天历史
 * @author djbian
 *
 */
public class GetChatHistory extends SimpleBaseCommand {
		
	private String conversationId;
	private int start;
	
	public GetChatHistory(String conversationId,int start) {
		this.conversationId = conversationId;
		this.start = start;
	}

	@Override
	public Result go() {
		Builder b = FindMessageListRequest.FindMessageListReq.newBuilder();
		b.setConversationId(conversationId);
		b.setRecordNo(start);
		b.setPageSize(10);
		return new HttpTemplate().postForGogoByte(AppConfigContext.TAPLINKER_WEB
				+ "/services/im/v1/messagelist"
				, b.build().toByteArray());
		
	}
	
	@Override
	public void yes(Result result) {
		super.yes(result);
		try {
			CrmDataCenter.getInstance().setMessageLists(MessageListResponse.MessageListRes.parseFrom((byte[]) result.object));
		} catch (InvalidProtocolBufferException e) {
			e.printStackTrace();
		}
		CrmDataCenter.getInstance().noticeListener(CrmDataCenter.GET_CHAT_HISTORY);
	}

	@Override
	public void no(Result result) {
		Toast.makeText(CrmDataCenter.getInstance().getContext(), "" + result.message, Toast.LENGTH_SHORT).show();
	}
	
}
